#_*_coding:utf-8 _*_
import sys
import os
import uuid
import json
from flask import jsonify, request, send_file, render_template,Flask,make_response
from model import Area

reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask("__name__")
@app.route('/mapcms/',methods = ["GET"])
def hello():

  content = []
  #a = [1,2,2,3,2,2,2,2,2,2,2,2,2,2]
  data = Area.query.filter_by(uuid="8f00a532-62d3-4a69-8155-d0c0ae7fddb7").order_by(Area.internal_name).all()
  nums = Area.query.filter_by(level = 0).all()
  for num in nums:
    info = {}
    info['key'] = num.internal_name
    content.append(info)
 # print 2222222
 # for area in data:
  # print area.internal_name,area.coordinates,area.parent
  return render_template('index.html',var = content)

@app.route('/mapcms/<string:internal_name>',methods = ["POST"])
def show(internal_name = "china"):
  #print '8'*8	 
  content = []
  #con = 0 
  data =request.json["name"]
  print 22222
  ll = Area.query.filter_by(internal_name = data).all()
  print ll[0].uuid
  nums = Area.query.filter_by(parent = ll[0].uuid).all()
  for num in nums:
    info = {}
    info['key'] = num.internal_name
    print info['key']
    content.append(info)
  con =  ll[0].level+1
  print content
  return jsonify(var1 = con)
  #return jsonify(con) 
  #response = make_response(render_template('index.html', var=404,var1=2))
  

if __name__ == '__main__':
 
   app.run(
     debug = True,
     port = 5000,
     host = "0.0.0.0" 
 
   )
